package asia.yuanding.wechat.web.netty.handler.factory;

import asia.yuanding.wechat.web.netty.handler.MessageHandlerUtil;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: MessageHandlerFactory
 * @Author: YuanDing
 * @Date: 2025/8/3 21:43
 * @Description:
 */

@Component
public class MessageHandlerFactory {

    private final Map<String, MessageHandlerUtil> handlerMap = new HashMap<>();

    // Spring 会自动注入所有实现了 MessageHandler 的 Bean
    public MessageHandlerFactory(List<MessageHandlerUtil> handlers) {
        for (MessageHandlerUtil handler : handlers) {
            handlerMap.put(handler.getType(), handler);
        }
    }

    public MessageHandlerUtil getHandler(String type) {
        return handlerMap.get(type);
    }
}

